from rest_framework import serializers
from .models import *                   # 验证错误      认证错误
from rest_framework.exceptions import ValidationError,AuthenticationFailed
import re


# 部门的序列化器
class DepartmentSer(serializers.Serializer):
    id = serializers.IntegerField(label="部门编号", read_only=True)
    name = serializers.CharField(label="部门名称", max_length=50, min_length=1, allow_null=True,
                                 # 去掉空格
                                 trim_whitespace=True)

    # 全局校验
    def valiadte(self, data):
        print("正在进行全局校验", data)

        return data

    # 重写create方法
    def create(self, validated_date):
        # 保存数据
        dep = Department.objects.create(**validated_date)

        return dep


def a(name):
    return name

# 职责的序列化器
class DutySer(serializers.Serializer):
    id = serializers.IntegerField(label="职责编号", read_only=True)                 # 不允许为空
    name = serializers.CharField(label="职责名称", max_length=50, min_length=1, allow_null=False, trim_whitespace=True
                                 ,validators=[a,])

    # 局部校验
    def validate_name(self, name):

        if re.findall(r"[A-Z]$", name):
            return name

        else:
            return ValidationError("职责名称不符合规则")

    # 全局校验
    def validate(self, data):
        # 校验所有的数据 data
        return data


# 员工序列化器
# class StaffSer(serializers.Serializer):
#     SEX = (
#         (0, "男"),
#         (1, "女")
#     )
#     id = serializers.IntegerField(label="员工id", read_only=True)
#     name = serializers.CharField(label="员工姓名", min_length=1, max_length=50, trim_whitespace=True)
#     sex = serializers.ChoiceField(choices=SEX, default=0)
#     phone = serializers.CharField(label="手机号", max_length=11, min_length=11, trim_whitespace=True)
#     email = serializers.CharField(label="邮箱", max_length=50)
#
#     department = serializers.PrimaryKeyRelatedField(queryset=Department.objects.all())
#     duty = serializers.PrimaryKeyRelatedField(queryset=Duty.objects.all())

class StaffSer(serializers.ModelSerializer):
    class Meta:
        model = Staff
        fields = "__all__"


# 用户的序列化器
class UserSer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True, trim_whitespace=True, max_length=50, min_length=2)

    # def validate(self, data):
    #     ...
    #     return data

    class Meta:
        model = User
        fields = ["id", "username", 'password', 'phone', "email", "date_joined", "last_login"]
        read_only_fields = ["id","date_joined", "last_login"]

    def update(self, instance, validated_date):
        instance.username = validated_date.get("username")
        instance.set_password(validated_date.get("password"))
        instance.phone = validated_date.get("phone")
        instance.email = validated_date.get("email")
        instance.save()
        return instance


# 用户头像的序列化器
class UserPortraitSer(serializers.ModelSerializer):
    class Meta:
        model = UserPortrait
        fields = "__all__"
        read_only_fields = ["id",]


# 权限的序列化器
class PermissionSer(serializers.ModelSerializer):
    typename = serializers.SerializerMethodField(read_only=True)

    def get_typename(self, obj):
        return obj.type_name.name

    class Meta:
        model = Permission
        fields = '__all__'
        read_only_fields = ['id',]


# 权限类型的序列化器
class PermissionTypeSer(serializers.ModelSerializer):
    class Meta:
        model = PermissionType
        fields = "__all__"
        read_only_fields = ["id",]


